#include "vibrator_controller.h"
#include "vibrator_controller_sprd_helper.h"
#include "hdf_base.h"
#include "hdf_log.h"
#include "osal_mem.h"

#define HDF_LOG_TAG             uhdf_vibrator_service
#define VIBRATOR_SERVICE_NAME   "hdf_misc_vibrator"

static struct VibratorDevice *GetVibratorDevicePriv(void)
{
    static struct VibratorDevice vibratorDeviceData = {
        .initState = false,
        .vibratorInfoEntry = {
            .isSupportIntensity = 0,
            .isSupportFrequency = 0,
            .intensityMaxValue = 0,
            .intensityMinValue = 0,
            .frequencyMaxValue = 0,
            .frequencyMinValue = 0,
        },
        .ioService = NULL,
    };

    return &vibratorDeviceData;
}

static int32_t GetVibratorInfo(struct VibratorInfo **vibratorInfo)
{
    struct VibratorDevice *priv = GetVibratorDevicePriv();

    *vibratorInfo = &priv->vibratorInfoEntry;

    return HDF_SUCCESS;
}

static int32_t EnableVibratorModulation(uint32_t duration, uint16_t intensity, int16_t frequency)
{
    HDF_LOGE("%s: Not supported", __func__);
    return HDF_ERR_NOT_SUPPORT;
}

static int32_t StartOnce(uint32_t duration)
{
    if (!duration)
        return HDF_SUCCESS;

    if (vibra_ff_on(duration)) {
        HDF_LOGE("%s: vibra_ff_on failer", __func__);
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}

static int32_t Start(const char *effect)
{
    // supported in the future
    HDF_LOGE("%s: Not supported", __func__);
    return HDF_ERR_NOT_SUPPORT;
}

static int32_t GetEffectInfo(const char *effect, struct EffectInfo *effectInfo)
{
    HDF_LOGE("%s: effect not support", __func__);
    effectInfo->isSupportEffect = false;
    effectInfo->duration = 0;

    return HDF_SUCCESS;
}

static int32_t Stop(enum VibratorMode mode)
{
    if (vibra_ff_off()) {
        HDF_LOGE("%s: vibra_ff_off failer", __func__);
        return HDF_FAILURE;
    }

    return HDF_SUCCESS;
}

const struct VibratorInterface *NewVibratorInterfaceInstance(void)
{
    static struct VibratorInterface vibratorDevInstance;
    struct VibratorDevice *priv = GetVibratorDevicePriv();

    if (priv->initState) {
        return &vibratorDevInstance;
    }

    OsalMutexInit(&priv->mutex);  // Reserved
    vibratorDevInstance.Start = Start;
    vibratorDevInstance.StartOnce = StartOnce;
    vibratorDevInstance.Stop = Stop;
    vibratorDevInstance.GetVibratorInfo = GetVibratorInfo;
    vibratorDevInstance.GetEffectInfo = GetEffectInfo;
    vibratorDevInstance.EnableVibratorModulation = EnableVibratorModulation;

    if (vibra_ff_exists()) {
        HDF_LOGD("Vibrator using force feedback");
    } else {
        HDF_LOGE("%s: Vibrator device does not exist. Cannot start vibrator",
                 __func__);
        OsalMutexDestroy(&priv->mutex);
        return NULL;
    }

    priv->initState = true;
    HDF_LOGD("get vibrator devInstance success");
    return &vibratorDevInstance;
}

int32_t FreeVibratorInterfaceInstance(void)
{
    struct VibratorDevice *priv = GetVibratorDevicePriv();

    if (!priv->initState) {
        HDF_LOGD("%s: vibrator instance had released", __func__);
        return HDF_SUCCESS;
    }

    OsalMutexDestroy(&priv->mutex);

    return HDF_SUCCESS;
}
